﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.IO;
using System.Windows;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Resources;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Tasks;

namespace PhoneUI.Controls
{
    public partial class ChoosePic : UserControl
    {
        private ObservableCollection<ShortCut> ShortCuts = new ObservableCollection<ShortCut>();
        private Popup popup;
        public event SelectionChangedEventHandler SelectonChanged;

        public ChoosePic()
        {
            InitializeComponent();
            LoadImage();
        }

        private void LoadImage()
        {
            string url = "/ShortCut/{0}.png";
            for (int i = 1; i < 85; i++)
            {
                string tmp = string.Format(url, i);
                ShortCut sc = new ShortCut();
                sc.ImagePath = tmp;
                ShortCuts.Add(sc);
            }
            IconList.ItemsSource = ShortCuts;
        }

        public void Show()
        {
            popup = new Popup();
            popup.Child = this;
            popup.IsOpen = true;
            popup.HorizontalOffset = 5;
            popup.VerticalOffset = 200;
        }

        public void Hide()
        {
            if (popup != null)
            {
                popup.IsOpen = false;
            }
        }

        private void IconList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SelectonChanged != null)
            {
                SelectonChanged.Invoke(sender, e);
            }
            popup.IsOpen = false;
        }
    }

    public class ShortCut
    {
        private string imagepath;

        private ImageSource image;

        public string ImagePath
        {
            get { return imagepath; }
            set
            {
                image = new BitmapImage(new Uri(value, UriKind.Relative));
                imagepath = value;
            }
        }

        public ImageSource Image
        {
            get { return image; }
        }
    }
}
